//
//  QuestionTableViewController.m
//  FreshBee
//
//  Created by 姜 祥云 on 16/10/16.
//  Copyright © 2016年 iOS013. All rights reserved.
//

#import "QuestionTableViewController.h"
#import "QuestionGroup.h"
#import "Question.h"
#import "QuestionHeaderTableView.h"

@interface QuestionTableViewController ()<QuestionHeaderTableViewDelegate>

@property (nonatomic,strong) NSArray *questionGroupArray;
@end

@implementation QuestionTableViewController
-(NSArray *)questionGroupArray
{
    if(_questionGroupArray == nil){
        _questionGroupArray =[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"HelpPlist" ofType:@"plist"]];
        NSMutableArray *nmArray = [NSMutableArray array];
        
        for(NSDictionary *dict in _questionGroupArray){
            NSLog(@"%@",dict);
            [nmArray addObject:[QuestionGroup questionWithDict:dict]];
            
            
        }
        _questionGroupArray =nmArray;
    }
    return _questionGroupArray;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden=NO;
    self.tableView.sectionHeaderHeight=50;
    self.tableView.tableFooterView = [[UIView alloc] init];
    
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return self.questionGroupArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    QuestionGroup *questionGroup=self.questionGroupArray[section];
        return questionGroup.open?questionGroup.texts.count : 0;

}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuseId=@"questionGroup";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    if(!cell){
        cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
    }
    QuestionGroup *questionGroup=self.questionGroupArray[indexPath.section];
    Question *question=questionGroup.texts[indexPath.row];
    
    cell.textLabel.text=questionGroup.texts[indexPath.row];
    
    cell.textLabel.numberOfLines = 0;
    
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    
    
    return cell;
}
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    // wrong
////    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//    QuestionGroup *questionGroup=self.questionGroupArray[indexPath.section];
//    Question *question=questionGroup.texts[indexPath.row];
//    
////    cell.textLabel.text=questionGroup.texts[indexPath.row];
//    
//        NSString *text = [question objectAtIndex:indexPath.row];
//    //320为文字显示的宽度，高度1000是随便写的，会自动根据文字的大小和宽度计算出高度
//    CGSize size = [cell.textLabel.text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(320, 1000)];
//    // +20是为了让每个cell之间有些间隔
//    return size.height+20;
//}


#pragma mark -- 加组标题


// 团购中 尾部和头View  是通过 self.tableView.tableHeaderView = view;
// 一个UITableView中只能有一个头 一个尾view

//下面的方法,是给UITableView中的组 设置头尾View
// 有多少个组 就可以有多少个头尾view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    NSString *reuseId = @"headerView";
    
    //通过这个方法,完成头尾view的重用
    QuestionHeaderTableView *headerView =   [tableView dequeueReusableHeaderFooterViewWithIdentifier:reuseId];
    
    if (!headerView) {
        headerView = [[QuestionHeaderTableView alloc]initWithReuseIdentifier:reuseId];
    }
    
    
#pragma mark -- 使用代理: 1. 设置代理对象
    headerView.delegate = self;
    
    
    //传入数据
    QuestionGroup *questionGroup = self.questionGroupArray[section];
    
    headerView.questionGroup  = questionGroup;
    
    
    
    return headerView;
    
    
    
    
}


#pragma mark -- 头view的协议

- (void)btnClickWithQuestionHeaderTableView:(QuestionHeaderTableView *)headerView{
    
    
    for (QuestionGroup *que in self.questionGroupArray) {
        if (que == headerView.questionGroup) {
            que.open = ! headerView.questionGroup.open;
        }else{
            que.open = NO;
        }
    }
    //2. 更改组内的标示
//    headerView.questionGroup.open = ! headerView.questionGroup.open;
    
    
    
    //3. 刷新UITableView;
    [self.tableView reloadData];
    
    
    
}



#pragma mark -- 隐藏状态栏

- (BOOL)prefersStatusBarHidden{
    return YES;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
